<template>
	<view class="container">
		<view class="tui-news-title">
			{{detail.title}}
		</view>
		<view class="tui-sub-info">
			<view class="tui-sub-left">
				<text class="tui-author" v-if="detail.author_info.nickname">{{detail.author_info.nickname}}</text>
				<text>更新时间:{{detail.updated_at.substring(0,10)}}</text>
			</view>
			<view class="tui-sub-right">阅读 {{detail.visiter}}</view>
		</view>
		<view class="tui-news-content">
			<view class="tui-article">
				<parser :html="detail.content"></parser>
			</view>
			

		</view>


		<view class="tui-operate-box">
			<tui-tag :type="isFabulous?'':'gray'" shape="circle" :plain="true" @click="btnFabulous">
				<tui-icon :name="iconName(isFabulous)" :size="20" :color="iconColor"></tui-icon>
				<text class="tui-black" :class="[isFabulous?'tui-primary':'']">{{detail.zan}}赞</text>
			</tui-tag>
			<button open-type="share" class="tui-share-btn">
				<tui-tag type="gray" shape="circle" :plain="true">
					<tui-icon name="partake" :size="20" color="#333"></tui-icon>
					<text class="tui-black">分享</text>
				</tui-tag>
			</button>

		</view>

		<view class="tui-cmt-title">评论</view>
		<view class="tui-cmtbox">
			<view class="tui-cmt-cell" v-for="(item,index) in cmtList" :key="index">
				<image :src="item.user_info.avatarUrl" class="tui-avatar"></image>
				<view class="tui-cmt-detail">
					<view class="tui-header-box">
						<view class="tui-cmt-nickname" v-if="item.user_info.nickname">{{item.user_info.nickname}}</view>
						<view class="tui-cmt-nickname" v-else>匿名</view>
						<!-- <view class="tui-fabulous" :class="[item.isFabulous?'tui-primary':'']" :id="index" @tap="cmtFabulous">
							<text>{{item.fabulous==0?'赞':item.fabulous}}</text>
							<tui-icon :name="iconName(item.isFabulous)" :size="15" :color="itemIconColor(item.isFabulous)"></tui-icon>
						</view> -->
					</view>
					<view class="tui-cmt-content">
						{{item.content}}
					</view>
				
					<view class="tui-footer">
						{{item.created_at}}
						<!-- <view class="tui-primary tui-ml" hover-class="opcity" :hover-start-time="150" @tap="cmtReply">回复</view> -->
					</view>
				</view>
			</view>

		</view>

		<view class="tui-operation">
			<view class="tui-operation-left tui-col-7 tui-height-full tui-ptop-zero">
				<view class="tui-btn-comment" @tap="btnCmt">发表你的猜想...</view>
			</view>
			<view class="tui-operation-right tui-right-flex tui-col-5">
				<view class="tui-operation-item" hover-class="opcity" :hover-stay-time="150" @tap="cmtAll">
					<tui-icon name="message" :size="30" color='#444'></tui-icon>
					<tui-badge type="white" size="small">{{detail.comment_count}}</tui-badge>
				</view>
				<!-- <view class="tui-operation-item" @tap="collection">
					<tui-icon :name="isCollection?'star-fill':'star'" :size="29" :color="isCollection?'#5677fc':'#444'"></tui-icon>
				</view> -->
				<view class="tui-operation-item" hover-class="opcity" :hover-stay-time="150">
					<button open-type="share" class="tui-share-btn">
						<tui-icon name="share" :size="31" color='#444'></tui-icon>
					</button>
				</view>
			</view>
		</view>

		<!--加载loadding-->
		<tui-loadmore :visible="loadding" :index="3" type="primary"></tui-loadmore>
		<tui-nomore :visible="!pullUpOn" bgcolor="#fff" text="没有更多回复"></tui-nomore>
		<!--加载loadding-->
		<view class="tui-safearea-bottom"></view>
	</view>
</template>

<script>
	import tuiIcon from "@/components/icon/icon"
	import tuiTag from "@/components/tag/tag"
	// import tuiListCell from "@/components/list-cell/list-cell"
	import tuiLoadmore from "@/components/loadmore/loadmore"
	import tuiNomore from "@/components/nomore/nomore"
	import tuiBadge from "@/components/badge/badge"
	import parser from "@/components/jyf-Parser/index"
	export default {
		components: {
			tuiIcon,
			tuiTag,
			// tuiListCell,
			tuiLoadmore,
			tuiNomore,
			tuiBadge,
			parser
		},
		data() {
			return {
				detail:{
					title:''
				},
				fabulous: 123,
				isFabulous: false,
				isCollection: false,
				cmtList: [],
				pageIndex: 1,
				loadding: false,
				pullUpOn: true,
				id:0,
				page:1
			}
		},
		computed: {
			iconColor() {
				return this.isFabulous ? '#5677fc' : '#333'
			},
			itemIconColor() {
				return function(isFabulous) {
					return isFabulous ? '#5677fc' : '#9a9a9a'
				}
			},
			iconName() {
				return function(isFabulous) {
					return isFabulous ? 'agree-fill' : 'agree'
				}
			}
		},
		methods: {
			btnFabulous:async function() {
				//await this.$api.newsZan({article_id:this.detail.id});
				if(!this.isFabulous){
					this.detail.zan += 1;
				}else{
					this.detail.zan -= 1;
				}
				
				this.isFabulous = !this.isFabulous
			},
			cmtFabulous: function(e) {
				let index = e.currentTarget.id;
				let fabulousObj = this.cmtList[index];
				let isFabulous = this.cmtList[index].isFabulous;
				let fabulous = this.cmtList[index].fabulous;
				let fabulousNum = isFabulous ? fabulous - 1 : fabulous + 1;
				this.$set(fabulousObj, "fabulous", fabulousNum);
				this.$set(fabulousObj, "isFabulous", !isFabulous);
			},
			collection: function() {
				this.isCollection = !this.isCollection
				if (this.isCollection) {
					this.tui.toast("收藏成功！");
				}
			},
			btnCmt: function() {
				let user = uni.getStorageSync('userInfo');
				if(user.is_weixin==0){
					uni.showToast({ title: '请授权微信登录参与评论', icon: 'none',duration:2000 });
					setTimeout(()=>{
						uni.switchTab({
							url:'../mine/mine'
						})
					},2000)
					return;
				}
				uni.setStorageSync('detail',this.detail);
				uni.navigateTo({
					url: '../newsDetail/news-cmt'
				})
			},
			cmtAll: function() {
				uni.navigateTo({
					url: '../newsDetail/news-cmt-list?id='+this.detail.id
				})
			},
			cmtReply: function() {
				uni.navigateTo({
					url: '../news-cmt-reply/news-cmt-reply'
				})
			},
			async getDetail(){
				let detail = await this.$api.Story_list;
				console.log(detail)
				// this.detail =detail;
				detail.forEach(item => {
					if (item.id == this.id) {
						this.detail =item;
					}
				});
				uni.setStorageSync('detail',detail.data)
				
			},
			async getComment(more=false){
				
				let cmts = await this.$api.newsCommentList;
				this.cmtList =more?cmts:this.cmtList.concat(cmts);
				if(cmts.length==10){
					this.pullUpOn = true
					this.page+=1;
				}else{
					this.loadding = false;
					this.pullUpOn = false
				}
			}
		},
		async onLoad(e) {
			this.id = e.id;
			this.loadding = true;
			this.getDetail();
			this.getComment(true);
			uni.$on('refresh',async (res)=>{
			this.loadding = true;
				this.page = 1;  
				this.getDetail();
				this.getComment(true);
			});
		},
		onPullDownRefresh() {
		this.page = 1;  
		  setTimeout(() => {
			  this.getDetail();
			  this.getComment(true);
		    uni.hideNavigationBarLoading();
		    // 停止下拉动作
		    uni.stopPullDownRefresh();
		  }, 1000);
		},
				
		onReachBottom() {
		  if(this.pullUpOn){
			  this.getComment();
		  }
		},
	}
</script>

<style lang="less">
	page {
		background: #fff;
		color: #333;
	}

	.container {
		padding: 40rpx 30rpx 110rpx 30rpx;
		box-sizing: border-box;
	}

	.tui-news-title {
		font-size: 48rpx;
		font-weight: 500;
		text-align: justify;
	}

	.tui-sub-info {
		padding-top: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		color: #999;
	}

	.tui-author {
		color: #5677fc;
		padding-right: 20rpx;
	}

	.tui-news-content {
		padding-top: 40rpx;
	}

	.tui-article {
		/* text-indent: 2em; */
		font-size: 34rpx;
		padding-bottom: 40rpx;
		line-height: 60rpx;
		text-align: justify;
		word-break: break-all;
		word-wrap: break-word;
	}

	.tui-article-pic {
		width: 100%;
		display: block;
		margin-bottom: 40rpx;
	}

	.tui-news-source {
		font-size: 24rpx;
		color: #999;
	}

	.tui-operate-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 60rpx 40rpx;
		box-sizing: border-box;
	}

	.tui-tag-class {
		min-width: 130rpx;
		padding: 20rpx 52rpx !important;
		font-size: 26rpx !important;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.tui-black {
		color: #333;
		padding-left: 12rpx;
	}

	.tui-cmt-title {
		font-size: 30rpx;
		font-weight: bold;
		position: relative;
	}

	.tui-cmt-title::after {
		content: '';
		position: absolute;
		left: -18rpx;
		top: 18%;
		width: 6rpx;
		height: 64%;
		background: #FA5C03;
	}

	.tui-cmtbox {
		padding-bottom: 20rpx;
	}

	.tui-cmt-cell {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		padding-top: 44rpx;
	}

	.tui-avatar {
		width: 64rpx;
		height: 64rpx;
		border-radius: 32rpx;
		display: block;
		flex-shrink: 0;
	}

	.tui-cmt-detail {
		width: 100%;
		padding-left: 16rpx;
		box-sizing: border-box;
	}

	.tui-header-box {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		font-size: 30rpx;
	}

	.tui-cmt-nickname {
		color: #5677fc;
	}

	.tui-fabulous {
		color: #9a9a9a;
	}

	.tui-fabulous text {
		padding-right: 4rpx;
		font-size: 24rpx
	}

	.tui-cmt-content {
		font-size: 32rpx;
		color: #333;
		text-align: justify;
		padding-top: 8rpx;
		word-break: break-all;
		word-wrap: break-word;
	}

	.tui-reply-box {
		border-radius: 8rpx;
		overflow: hidden;
		margin-top: 16rpx;
	}

	.tui-cell-class {
		flex-direction: column;
		justify-content: flex-start !important;
		padding: 20rpx !important;
		text-align: justify;
		word-break: break-all;
		word-wrap: break-word;
	}

	.tui-cell-last {
		color: #5677fc;
	}

	.tui-flex-1 {
		flex: 1;
		width: 100%;
	}

	.tui-reply-nickname {
		font-size: 24rpx;
		color: #7a7a7a;
		padding-bottom: 8rpx;
	}

	.tui-footer {
		display: flex;
		align-items: center;
		font-size: 24rpx;
		margin-top: 16rpx;
		color: #9a9a9a;
	}

	.tui-primary {
		color: #5677fc !important;
	}

	.tui-ml {
		margin-left: 16rpx;
	}

	.tui-cell-last .tui-icon-class {
		width: 40rpx !important;
		margin-left: -10rpx;
	}

	.tui-operation {
		width: 100%;
		height: 100rpx;
		overflow: hidden;
		background: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99999;
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-safearea-bottom {
		width: 100%;
		height: env(safe-area-inset-bottom);
	}

	.tui-operation::before {
		content: '';
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		border-top: 1rpx solid #eaeef1;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}

	.tui-operation-left {
		display: flex;
		align-items: center;
	}

	.tui-operation-item {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		position: relative;
	}

	.tui-operation-right {
		height: 100rpx;
		box-sizing: border-box;
		padding-top: 0;
	}

	.tui-badge-class {
		color: #FA5C03 !important;
		position: absolute;
		top: -6rpx;
		padding: 2px 4px !important;
		/* #ifdef H5 */
		transform: translateX(50%) scale(0.8);
		/* #endif */
	}

	.tui-right-flex {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.tui-btn-comment {
		height: 64rpx;
		width: 84%;
		background: #ededed;
		color: #999;
		border-radius: 8rpx;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		padding-left: 20rpx;
		box-sizing: border-box;
		padding-top: 0;
		margin-left: 30rpx;
	}

	.tui-col-7 {
		width: 58.33333333%;
	}

	.tui-col-5 {
		width: 41.66666667%;
	}

	.tui-share-btn {
		display: block;
		background: none;
		margin: 0;
		padding: 0;
		border: none;
		&::after{
			display: none;
		}
	}
</style>
